package com.itheima.mapper;

import com.itheima.domain.User;
import org.apache.ibatis.annotations.*;

import java.io.IOException;
import java.util.List;

public interface UserMapper {
    @Select("select * from user")
    public List<User> findAll() throws IOException;
    @Select("select * from user where id = #{id}")
    public User findById(int id) throws IOException;
    @Update("update user set username = #{username},password = #{password} where id = #{id}")
    public void update(User user) throws IOException;
    @Delete("delete from user where id = #{id}")
    public void delete(int id) throws IOException;
    @Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
    public void save(User user) throws IOException;
    @Select("select * from user")
    @Results(
            value = {
                    @Result(id = true,column = "id",property = "id"),
                    @Result(column = "username",property = "username"),
                    @Result(column = "password",property = "password"),
                    @Result(column = "birthday",property = "birthday"),
                    @Result(
                            property = "orderList",//受封装的属性名称
                            column = "id",//根据那个字段去查询order表的数据
                            javaType = List.class,//要封装的实体类型
                            many = @Many(select = "com.itheima.mapper.OrderMapper.findByUid")
                    )
            }
    )
    public List<User> findUserAndOrderAll() throws IOException;
    @Select("select * from user")
    @Results(
            value = {
                    @Result(id = true,column = "id",property = "id"),
                    @Result(column = "username",property = "username"),
                    @Result(column = "password",property = "password"),
                    @Result(column = "birthday",property = "birthday"),
                    @Result(
                            property = "roleList",//受封装的属性名称
                            column = "id",//根据那个字段去查询role表的数据
                            javaType= List.class,//要封装的实体类型
                            many = @Many(select = "com.itheima.mapper.RoleMapper.findByUid")
                    )
            }
    )
    public List<User> findUserAndRoleAll() throws IOException;
}
